package com.huike.service;

import com.huike.dao.P2pDAO;
import com.huike.dao.UserDAO;
import com.huike.po.p2pMessage;
import com.huike.pojo.FileMsg;
import com.huike.pojo.MsgUpdateObject;
import com.huike.utils.FileUtils;
import com.huike.websocket.ChatWebSocketHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.sql.rowset.serial.SerialBlob;
import java.io.IOException;
import java.sql.Blob;
import java.sql.SQLException;
import java.util.List;

@Service
public class P2pServiceImpl implements P2pService {
    @Autowired
    private P2pDAO p2pDAO;
    @Autowired
    private UserDAO userDAO;
    @Autowired
    private ChatWebSocketHandler webSocket;
    @Autowired
    private MsgUpdateObject msgUpdateObject;

    @Override
    public List<p2pMessage> getChatRecord(String accountFirst, String accountSecond) {
        List<p2pMessage> p2pMessageList =p2pDAO.selectAllP2PChatRecordByUser(accountFirst,accountSecond);
        return p2pMessageList;
    }

    @Override
    public boolean sendMessage(p2pMessage message) {
        switch (message.getMsgType()){
            case "text" :
                message.setMsgTypeId(1);
                break;
            case "file" :
                message.setMsgTypeId(2);
                break;
            case "image" :
                message.setMsgTypeId(3);
                break;
            case "audio" :
                message.setMsgTypeId(4);
                break;
        }
        if (message.getMsgClient().equals("win")) {
            message.setMsgClientId(1);
        } else if (message.getMsgClient().equals("mobile")) {
            message.setMsgClientId(2);
        }
        // 判断黑名单
        int isBlacked = p2pDAO.selectIsBlackedByAccountAndFriendAccount(message);
        if(isBlacked == 1) return false;

        // 插入一条聊天记录
        p2pDAO.insertP2PChatRecord(message);
        // 判断接收方在线状态
        int onlineNum = userDAO.selectOnlineNumByAccount(message.getMsgTo());
        if( onlineNum >0) {
            //设置状态更新对象
            msgUpdateObject.setUpdateType("UpdateP2PMessage");
            msgUpdateObject.setUpdateContent(message);
            msgUpdateObject.setUpdateFrom(message.getMsgFrom());
            //调用ChatWebSocketHandler类中的自定义方法，修改好友的用户在线状态
            webSocket.sendUpdateToUser(msgUpdateObject,message.getMsgTo());
        }
        return true;
    }

    @Override
    public boolean deleteOfflineMsg(String accountFrom, String accountTo) {
        try{
            p2pDAO.deleteOfflineP2PRecord(accountFrom, accountTo);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    public List<p2pMessage> getRoamingRecord(String accountFirst, String accountSecond) {
        List<p2pMessage> p2pMessageList =p2pDAO.selectAllP2PRoamingRecordByUser(accountFirst, accountSecond);
        return p2pMessageList;
    }


    @Override
    public boolean sendFile(FileMsg message, MultipartFile file)  {
        if (message.getMsgClient().equals("win")) {
            message.setMsgClientId(1);
        } else if (message.getMsgClient().equals("mobile")) {
            message.setMsgClientId(2);
        }
        // 判断黑名单
        p2pMessage p2p = new p2pMessage();
        p2p.setMsgFrom(message.getMsgFrom());
        p2p.setMsgTo(message.getMsgTo());
        int isBlacked = p2pDAO.selectIsBlackedByAccountAndFriendAccount(p2p);
        if(isBlacked == 1) return false;

        message.setFileContent(FileUtils.MultiPartFileToBlob(file));
        try {
            message.setFileBytes(file.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        p2pDAO.insertFileRecord(message);
        // 判断接收方在线状态
        int onlineNum = userDAO.selectOnlineNumByAccount(message.getMsgTo());

        if( onlineNum > 0) {
            msgUpdateObject.setUpdateType("UpdateP2PMessage");
            msgUpdateObject.setUpdateContent(message);
            msgUpdateObject.setUpdateFrom(message.getMsgFrom());
            webSocket.sendUpdateToUser(msgUpdateObject, message.getMsgTo());
        }
        return true;
    }
    @Override
    public List<FileMsg> getFileList(String msgFrom, String msgTo) {
        List<FileMsg> message = p2pDAO.selectFileRecord(msgFrom, msgTo);
        return message;
    }

    @Override
    public FileMsg getFile(String msgFrom, String msgTo, String fileName) {
        List<FileMsg> message = p2pDAO.selectFileByFileName(msgFrom, msgTo, fileName);
        if(message.size() == 0)
            return null;
        return message.get(0);
    }
}
